import Vue from 'vue'
import Router from 'vue-router'
// 审批
import approvalRoute from './modules/approval'
// 考勤
import attendanceRoute from './modules/attendance'
// 组织架构
import departmentRoute from './modules/department'
// 员工
import employeeRoute from './modules/employee'
// 权限
import permissionRoute from './modules/permission'
// 角色
import roleRoute from './modules/role'
// 薪资
import salaryRoute from './modules/salary'
// 社保
import socialRoute from './modules/social'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    name: 'LoginPage',
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    name: '404Page',
    path: '/404',
    component: () => import('@/views/404'),
    // 不会显示在左侧菜单中
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    // 当 children 有多个时，该路由配置会在左侧菜单中渲染成二级结构
    children: [{
      // 不以 / 开头，会带上父路由的 path => /dashboard
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      // 路由元数据：用来在路由对象（$route）上携带一些额外的信息
      // title：左侧菜单项名称
      // icon：左侧菜单项的图标 => 可以是 @/icons/svg/文件名，还可以是 ElementUI 自带的图标类名
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  // 快捷键：
  // ctrl + z：撤销
  // ctrl + y：恢复
  // alt + 上下键：上下移动选中的代码
  // alt + shift + 上下键：快速复制选中的代码至上面或下面
  // ctrl + D：选中下一个相同的内容
  // 按住 alt + 鼠标左击：添加多个光标
  // 按住 alt + shift + 鼠标左击拖拽：可以拉出一排直线的光标
  // ctrl + 左右方向键：将光标快速移动一段文本
  // home + end：将光标移动到最前或最后

  approvalRoute,
  attendanceRoute,
  departmentRoute,
  employeeRoute,
  permissionRoute,
  roleRoute,
  salaryRoute,
  socialRoute,

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
